﻿using System;
using Microsoft.Xna.Framework;

partial class Player :  AnimatedGameObject
{
    float groundFriction = 1.2f;
    float walkingSpeed = 90;
    public void Jump(float speed = 1800)
    {
        velocity.Y = -speed;
        GameEnvironment.AssetManager.PlaySound("Sounds/snd_player_jump");
    }
    public void DoubleJump()
    {
        Jump();
        doubleJumped = true;
        (parent as Level).Timer.TimeLeft -= TimeSpan.FromSeconds(doublejumpCosts);

        doubleJumpAnimation.Position = this.position;
        doubleJumpAnimation.Reset();
        doubleJumpAnimation.PlayAnimation("explode");

        //Kill enemies in a radius
        float radius = 200;
        GameObjectList enemies = (parent as GameObjectList).Find("enemies") as GameObjectList;
        foreach (GameObject obj in enemies.Objects)
        {
            if (radius > Math.Sqrt(Math.Pow(obj.GlobalPosition.X - this.GlobalPosition.X, 2) + Math.Pow(obj.GlobalPosition.Y - this.GlobalPosition.Y, 2)))
            {
                PatrollingEnemy enemy = obj as PatrollingEnemy;

                if (enemy != null)
                {
                    enemy.Kill();
                    continue;
                }

                Rocket rocket = obj as Rocket;

                if (rocket != null)
                {
                    rocket.Kill();
                    continue;
                }
            }
        }
    }
    
    private void DoPhysics()
    {
        if (!exploded)
        {
            //Movement
            if (walkingRight)
                velocity.X += walkingSpeed;
            else if (walkingLeft)
            {
                velocity.X -= walkingSpeed;
            }
            velocity.Y += 100;

            if (isOnTheGround && walkingOnIce)
                velocity /= (groundFriction - 1) / 2 + 1;
            else if (isOnTheGround)
                velocity /= groundFriction;

            velocity = new Vector2((int)velocity.X, (int)velocity.Y);
        }
        if (isAlive)
            HandleCollisions();
    }

    private void HandleCollisions()
    {
        isOnTheGround = false;
        walkingOnIce = false;
        walkingOnHot = false;

        TileField tiles = GameWorld.Find("tiles") as TileField;
        int x_floor = (int)position.X / tiles.CellWidth;
        int y_floor = (int)position.Y / tiles.CellHeight;

        for (int y = y_floor - 2; y <= y_floor + 1; ++y)
            for (int x = x_floor - 1; x <= x_floor + 1; ++x)
            {
                TileType tileType = tiles.GetTileType(x, y);
                if (tileType == TileType.Background)
                    continue;
                Tile currentTile = tiles.Get(x, y) as Tile;
                Rectangle tileBounds = new Rectangle(x * tiles.CellWidth, y * tiles.CellHeight,
                                                        tiles.CellWidth, tiles.CellHeight);
                Rectangle boundingBox = this.BoundingBox;
                boundingBox.Height += 1;
                if (((currentTile != null && !currentTile.CollidesWith(this)) || currentTile == null) && !tileBounds.Intersects(boundingBox))
                    continue;
                Vector2 depth = Collision.CalculateIntersectionDepth(boundingBox, tileBounds);
                if (Math.Abs(depth.X) < Math.Abs(depth.Y))
                {
                    if (tileType == TileType.Normal)
                        position.X += depth.X;
                    continue;
                }
                if (previousYPosition <= tileBounds.Top && tileType != TileType.Background)
                {
                    isOnTheGround = true;
                    doubleJumped = false;
                    velocity.Y = 0;
                    if (currentTile != null)
                    {
                        walkingOnIce = walkingOnIce || currentTile.Ice;
                        walkingOnHot = walkingOnHot || currentTile.Hot;
                    }
                }
                if (tileType == TileType.Normal || isOnTheGround)
                    position.Y += depth.Y + 1;             
            }
        position = new Vector2((float)Math.Floor(position.X), (float)Math.Floor(position.Y));
        previousYPosition = position.Y;
    }
}